package com.example.academic_affairs_system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.academic_affairs_system.common.RPage;
import com.example.academic_affairs_system.mapper.AasClassStudentMapper;
import com.example.academic_affairs_system.model.dto.classStudent.ClassStudentPageDTO;
import com.example.academic_affairs_system.model.entity.AasClassStudent;
import com.example.academic_affairs_system.model.vo.classStudent.ClassStudentVO;
import com.example.academic_affairs_system.model.vo.courses.CoursesVO;
import com.example.academic_affairs_system.service.IAasClassCoursesService;
import com.example.academic_affairs_system.service.IAasClassStudentService;
import com.example.academic_affairs_system.service.IAasStudentCoursesService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务实现类
 *
 * @author hjx
 * @since 2023-01-31
 */
@Service
public class AasClassStudentServiceImpl extends ServiceImpl<AasClassStudentMapper, AasClassStudent>
    implements IAasClassStudentService {
  @Resource HttpServletRequest request;
  @Resource IAasStudentCoursesService iAasStudentCoursesService;
  @Resource IAasClassCoursesService iAasClassCoursesService;

  @Override
  public long getClassIdByStudentId(long userId) {
    AasClassStudent aasClassStudent =
        baseMapper.selectOne(
            Wrappers.<AasClassStudent>lambdaQuery().eq(AasClassStudent::getStudentId, userId));
    if (aasClassStudent == null) {
      return -1;
    }
    return aasClassStudent.getClassId();
  }

  @Override
  public AasClassStudent getClassStudentByStudentId(long userId) {
    return baseMapper.selectOne(
        Wrappers.<AasClassStudent>lambdaQuery().eq(AasClassStudent::getStudentId, userId));
  }

  @Override
  public List<AasClassStudent> getClassStudentByClassId(long classId) {
    return baseMapper.selectList(
        Wrappers.<AasClassStudent>lambdaQuery().eq(AasClassStudent::getClassId, classId));
  }

  @Override
  public List<AasClassStudent> getClassStudentByClassIds(List<Long> classIds) {
    return baseMapper.selectList(
        Wrappers.<AasClassStudent>lambdaQuery().in(AasClassStudent::getClassId, classIds));
  }

  @Override
  public RPage<ClassStudentVO> getClassStudentVOById(ClassStudentPageDTO dto) {
    return RPage.<ClassStudentVO>createDto(dto)
        .execute((page) -> baseMapper.getClassStudentVOByClassId(page, dto))
        .build();
  }

  @Override
  public long addStudentToClass(AasClassStudent classStudent) {
    baseMapper.insert(classStudent);
    // 如果班级绑定了课程，那么学生与课程进行绑定
    List<CoursesVO> classCoursesVOList =
        iAasClassCoursesService.getClassCoursesVOList(classStudent.getClassId());
    if (CollectionUtil.isNotEmpty(classCoursesVOList)) {
      iAasStudentCoursesService.batchRelated(
          Collections.singletonList(classStudent.getStudentId()),
          classCoursesVOList.stream().map(CoursesVO::getCoursesId).collect(Collectors.toSet()));
    }
    return classStudent.getId();
  }
}
